package cn.sxt.questions.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.sxt.common.test.MultiThreadUtil;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.YxlSpiderUtil;
import cn.sxt.questions.pojo.Columns;
import cn.sxt.questions.pojo.Items;
import cn.sxt.questions.service.IQuestionService;
import cn.sxt.questions.service.IQuestionSpiderService;
import cn.sxt.users.pojo.POJOEnum;

@Service("questionSpiderService")
public class IQuestionSpiderServiceImpl implements IQuestionSpiderService{
	@Resource
	private IQuestionService questionService;
	
	@Override
	public JSONObject saveBatchItemsService(String url) {
		ConstatFinalUtil.LOGGER.info("---saveBatchItemsService----");
		JSONObject resultJSON=new JSONObject();
		Map<String,Object> condMap=new HashMap<String,Object>();
		List<Columns> columnsList=new ArrayList<Columns>();
		if(url==null||"".equals(url)){
			condMap.put("status", POJOEnum.STATUS_ENABLE.getStatus());
			columnsList=this.questionService.findListColumnsService(condMap, null);
		}else{
			condMap.put("url", url);
			Columns columns=this.questionService.findOneColumnsService(condMap);
			columnsList.add(columns);
		}
		/*根据栏目选择合适的测试项目*/
		for (Iterator iterator = columnsList.iterator(); iterator.hasNext();) {
			Columns columns = (Columns) iterator.next();
			YxlSpiderUtil yxlSpiderUtil=new YxlSpiderUtil();
			JSONObject res=yxlSpiderUtil.itemsList(columns.getUrl(),1);	
			int page=res.getJSONObject("data").getInteger("totalPage");
			System.out.println(page);
			/*第一次主要查询分页的数据*/
			/*判断状态码是否为0，然后在继续进行查询*/
			if("0".equalsIgnoreCase(res.getString("code"))){
				/*证明查询没有问题*/
				/*接着查询下面所有的页数*/
				for(int i=1;i<=page;i++){
					/*先获取data*/
					JSONObject itemsListJSON=yxlSpiderUtil.itemsList(columns.getUrl(), i).getJSONObject("data");
					/*再获取itemsList*/
					JSONArray dataJSON=itemsListJSON.getJSONArray("itemsList");
					for (Iterator iterator2 = dataJSON.iterator(); iterator2.hasNext();) {
						JSONObject JSONTemp = (JSONObject) iterator2.next();
						/*壹心理网站的id*/
						int outId=JSONTemp.getIntValue("id");
						/*根据outerId查询这些数据是否已经存在*/
						/*本质就是ID,对原先的数据进行查询*/
						condMap.clear();
						condMap.put("id", outId);
						/*这里对Items查询*/
						Items items=this.questionService.findOneItemsService(condMap);
						boolean update=true;
						if(items==null){
							items=new Items();
							update=false;
						}
						try {
							items.setName(JSONTemp.getString("name"));
							items.setContent(JSONTemp.getString("content"));
							items.setStatus(Byte.parseByte("1"));
							items.setPubTime(new Date());
							items.setCreateTime(new Date());
							items.setUpdateTime(new Date());
							items.setFavNum(Integer.parseInt(JSONTemp.getString("favNum")));
							items.setImgPath(JSONTemp.getString("imgPath"));
							items.setSuggNum(Integer.parseInt(JSONTemp.getString("suggNum")));
							items.setQuestionNum(Integer.parseInt(JSONTemp.getString("questionNum")));
							items.setTestedNum(JSONTemp.getInteger("testedNum"));
							items.setId(JSONTemp.getInteger("id"));
						} catch (NumberFormatException e) {
							e.printStackTrace();
							/*进行一定处理*/
						}
						if(update){
							this.questionService.updateService(items);
						}else{
							resultJSON=this.questionService.saveService(items);
						}
					}
				}
			}
			
		}
		return resultJSON;
	}
	/**
	 * 这里重写了方法，有几个栏目就开几个线程
	 */
	public JSONObject saveBatchItemsBak(String url){
		ConstatFinalUtil.LOGGER.info("--saveBatchItemsService--");
		JSONObject resultJSON=new JSONObject();
		Map<String,Object> condMap=new HashMap<String,Object>();
		List<Columns> columnsList=new ArrayList<Columns>();
		if(url==null||"".equalsIgnoreCase(url)){
			condMap.put("status", POJOEnum.STATUS_ENABLE.getStatus());
			columnsList=this.questionService.findListColumnsService(condMap, null);
			List<MultiThreadUtil> threadList=new ArrayList<MultiThreadUtil>();
			for (Iterator<Columns> iterator = columnsList.iterator(); iterator.hasNext();) {
				Columns columns = iterator.next();
				/*创建线程*/
				MultiThreadUtil multiThreadUtil=new MultiThreadUtil("");
	 			Map<String,Object> paramsMap=new HashMap<String,Object>();
				paramsMap.put("operType", "spliderColumns");
				paramsMap.put("url", columns.getUrl());
				paramsMap.put("questionSpiderService", this);
				multiThreadUtil.setParamsMap(paramsMap);
				threadList.add(multiThreadUtil);
			}
			ExecutorService executorService=Executors.newFixedThreadPool(threadList.size());
			try {	
				executorService.invokeAll(threadList);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				executorService.shutdown();
			}
		}else{
			condMap.put("url", url);
			Columns columns=this.questionService.findOneColumnsService(condMap);
			this.saveBatchItemsService(columns.getUrl());
		}
		
		return resultJSON;
	}
	
	
	
}








































